//Widgetpp.hpp
#pragma once
/*    widgetpp - A simple C++ wrapper of DISLIN graphic library               */
/*                                                                            */
/*    Copyright (C) 2012  Mirko Cambi (email: mirko.cambi@gmail.com)          */
/*                                                                            */
/*    This program is free software: you can redistribute it and/or modify    */
/*    it under the terms of the GNU General Public License as published by    */
/*    the Free Software Foundation, either version 3 of the License, or       */
/*    (at your option) any later version.                                     */
/*                                                                            */
/*    This program is distributed in the hope that it will be useful,         */
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of          */
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           */
/*    GNU General Public License for more details.                            */
/*                                                                            */
/*    You should have received a copy of the GNU General Public License       */
/*    along with this program.  If not, see <http://www.gnu.org/licenses/>.   */

#include <string>
#include <map>

#include <boost/config.hpp>
#include <boost/shared_ptr.hpp>

#include "dislin.h"
#include "Wdg.hpp"

namespace wdgpp {

namespace laid_out {
   extern const unsigned int 
      vert,
      hori,
      form;
}

class WdgRoot
{
public:
   virtual void draw() const = 0;
};

class Widgetpp: public WdgRoot
{
public:   
   Widgetpp(const std::string &title, int width, unsigned int lo);

   virtual ~Widgetpp();
   
   int add(const std::string &name, Wdg *wdg, int ip = -999);
   int id() const;

   
   template <class T>
   void register_callback(int id, T wcb)
   {
      swgcbk(id, &Widgetpp::global_cb);
      WDGCBS::instance().insert(std::make_pair(id, std::make_pair(this, reinterpret_cast<WdgCallback>(wcb))));
   }
   
   void set_focus(int id);
   void draw() const;
   
   template<class T>
   boost::shared_ptr<T> get_widget(const std::string &name)
   {
      return boost::static_pointer_cast<T>(_wdgs[name]);
   }
   
   static void global_cb(int id)
   {
      wdg_cb_map_type::const_iterator pos = WDGCBS::instance().find(id);
      if(pos == WDGCBS::instance().end())
         throw;
      
      ((*(pos->second.first)).*(pos->second.second))(id);
   }

private:
   int _id;
   int _focus;
   std::map<std::string, boost::shared_ptr<Wdg> > _wdgs;
};

} // namespace wdgpp

